﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using woaC2.LogicLib.DataContract.Input;
using woaPoker.LogicLib.Database;
using woaC2.LogicLib.GlobalShare;
using woaC2.SocketLib;
using woaC2.LogicLib.DataContract.Output;
using woaC2.LogicLib;
using Newtonsoft.Json;
using woaC2.Common;


namespace woaPoker.LogicLib.LogicCtrl
{
    class CreateGameLgCtrl
    {
        public CreateRoomOutDc CreateRoom(Guid sessionId,CreateRoomInDC obj)
        {
            Player player=PlayerRegistry.instance.GetPlayer(sessionId);
            Match match = new Match(player, obj.type, obj.password, obj.matchName);
            MathRegistry.instance.AddMatch(match);
            CreateRoomOutDc a = new CreateRoomOutDc();
            a.sessionIdMatch = match.sessionIdMatch;
            a.matchName = match.MatchName;
            //TestDC te = new TestDC();
            //te.content = "Hello";
            var jsonOut = "mes101$" + JsonConvert.SerializeObject(a);
            var mesOut =FunctionCmn.StampMessage(jsonOut);
            SocketServer.Instance.Broadcast(sessionId, mesOut);
            return a;
        }

        public JoinGameOutDC JoinGame(Guid sessionId,JoinGameInDC joinGameInDC)
        {
            Match match=MathRegistry.instance.GetMatchById(joinGameInDC.sessionIdMatch);
            Player player = PlayerRegistry.instance.GetPlayer(sessionId);
            BroadcastJoinGameOutDC objBroadcast=new BroadcastJoinGameOutDC();
            JoinGameOutDC b = new JoinGameOutDC();
            int a = MathRegistry.instance.AddPlayer(player, joinGameInDC.sessionIdMatch);
            if (a != 0)
            {
                objBroadcast.team = a;
                objBroadcast.userId = sessionId;
                var jsonOut = "mes103$" + JsonConvert.SerializeObject(objBroadcast);
                var mesOut = FunctionCmn.StampMessage(jsonOut);
                SocketServer.Instance.Broadcast(match, sessionId, mesOut);
                b.success = true;
                return b;
            }
            else
            {
                b.success = false;
                return b;
            }
        }
    }
}
